from ipaddress import ip_address
#from python_ping import ping
import os
import platform, subprocess

def ping(host_or_ip, packets=1, timeout=1):
	''' Calls system "ping" command, returns True if ping succeeds.
	Required parameter: host_or_ip (str, address of host to ping)
	Optional parameters: packets (int, number of retries), timeout (int, ms to wait for response)
	Does not show any output, either as popup window or in command line.
	Python 3.5+, Windows and Linux compatible (Mac not tested, should work)
	'''
	# The ping command is the same for Windows and Linux, except for the "number of packets" flag.
	if platform.system().lower() == 'windows':
		command = ['ping', '-n', str(packets), '-w', str(timeout), host_or_ip]
		# run parameters: capture output, discard error messages, do not show window
		result = subprocess.run(command, stdin=subprocess.DEVNULL, stdout=subprocess.PIPE, stderr=subprocess.DEVNULL, creationflags=0x08000000)
		# 0x0800000 is a windows-only Popen flag to specify that a new process will not create a window.
		# On Python 3.7+, you can use a subprocess constant:
		#   result = subprocess.run(command, capture_output=True, creationflags=subprocess.CREATE_NO_WINDOW)
		# On windows 7+, ping returns 0 (ok) when host is not reachable; to be sure host is responding,
		# we search the text "TTL=" on the command output. If it's there, the ping really had a response.
		return result.returncode == 0 and b'TTL=' in result.stdout
	else:
		command = ['ping', '-c', str(packets), '-w', str(timeout), host_or_ip]
		# run parameters: discard output and error messages
		result = subprocess.run(command, stdin=subprocess.DEVNULL, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
		return result.returncode == 0

def findIPs(start, end):
	start = ip_address(start)
	end = ip_address(end)
	result = []
	while start <= end:
		ip = str(start)
		print("ping %s"%ip)
		ret = ping(ip) #每个ip ping 1次，等待时间为1s
		if ret :
			print('ping ok')
		else:
			print('ping fail')

		result.append(str(start))
		start += 1

	return result



if __name__ == '__main__':
	# print(ping('192.168.1.2'))
	# print(ping('192.168.1.64'))
	findIPs('192.168.1.0', '192.168.1.255')